#include <utils.h>
#include <types.h>
#include <errno.h>

char caracters[] ={'0','1','2','3','4','5', '6','7','8','9'};

void itoaRecursiva(int *i, int num, char *buffer)
{
	if( (num/10) == 0 )
	{
	    *i++;
		buffer[*i]= caracters[num%10];
		*i--;
	}
	else
	{
		int residu = num%10;
		*i++;
		itoaRecursiva(i, num/10, buffer);
		buffer[*i] = caracters[residu];
		*i--;
	}
}

void itoa(int num, char *buffer) 
{
	int i;
	char aux[33];
	int index = 0;
	do {
	    aux[index++] = caracters[num%10];
	    num /= 10;
	} while (num > 0);
	for (i = 0; i<index; i++) {
	    buffer[i] = aux[index-i-1];
	}
	buffer[index] = NULL;
}

void copy_data(void *start, void *dest, int size)
{
  DWord *p = start, *q = dest;
  Byte *p1, *q1;
  while(size > 4) {
    *q++ = *p++;
    size -= 4;
  }
  p1=(Byte*)p;
  q1=(Byte*)q;
  while(size > 0) {
    *q1++ = *p1++;
    size --;
  }
}
/* Copia de espacio de usuario a espacio de kernel, devuelve 0 si ok y -1 si error*/
int copy_from_user(void *start, void *dest, int size)
{
  DWord *p = start, *q = dest;
  Byte *p1, *q1;
  while(size > 4) {
    *q++ = *p++;
    size -= 4;
  }
  p1=(Byte*)p;
  q1=(Byte*)q;
  while(size > 0) {
    *q1++ = *p1++;
    size --;
  }
  return 0;
}
/* Copia de espacio de kernel a espacio de usuario, devuelve 0 si ok y -1 si error*/
int copy_to_user(void *start, void *dest, int size)
{
  DWord *p = start, *q = dest;
  Byte *p1, *q1;
  while(size > 4) {
    *q++ = *p++;
    size -= 4;
  }
  p1=(Byte*)p;
  q1=(Byte*)q;
  while(size > 0) {
    *q1++ = *p1++;
    size --;
  }
  return 0;
}

/* access_ok: Checks if a user space pointer is valid
 * @type:  Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
 *         %VERIFY_WRITE is a superset of %VERIFY_READ: if it is safe
 *         to write to a block, it is always safe to read from it
 * @addr:  User space pointer to start of block to check
 * @size:  Size of block to check
 * Returns true (nonzero) if the memory block may be valid,
 *         false (zero) if it is definitely invalid
 */
int access_ok(int type, const void * addr, unsigned long size)
{
  return 0;
}
//Devuelve 0 si ok, devuelve el codigo de error en negativo si falla
int comprova_fd(int fd, int operacio) {
    if (fd != 1) return -EBADF;
    if (operacio != LECTURA || operacio != ESCRIPTURA) return -EPERM;
    return 0;
}
